package com.example.websocket.service.impl;

import com.example.websocket.mapper.FriendsMapper;
import com.example.websocket.pojo.dto.FriendDto;
import com.example.websocket.pojo.dto.NoteDto;
import com.example.websocket.pojo.dto.UserListDto;
import com.example.websocket.pojo.po.RequestPo;
import com.example.websocket.service.FriendsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ author 韩
 * time: 2024/3/11 18:58
 */
@Service
public class FriendsServiceImpl implements FriendsService {
    @Autowired
    private FriendsMapper friendsMapper;

    @Override
    public List<UserListDto> getOwnFriends(String username) {
        return friendsMapper.selectFriendsByUserName(username);
    }

    @Override
    public Boolean updateNote(NoteDto dto) {
        return friendsMapper.updateNote(dto.getUsername(),dto.getFriendName(),dto.getNote());
    }

    @Override
    public List<UserListDto> searchFriend(String username,String key) {
        return friendsMapper.searchFriend(username,key);
    }

    @Override
    public List<RequestPo> selectMyRequest(String username) {
        return friendsMapper.selectMyRequest(username);
    }

    @Override
    public Boolean agreeRequest(String sender_username, String receiver_name) {
         return friendsMapper.agreeRequest(sender_username,receiver_name);
    }

    @Override
    public Boolean deletedFriend(String deletedName, String username) {
        // 采用qq的实现方式，双向删除，微信则是单向删除
        Boolean flag1 = friendsMapper.deletedFriend(deletedName, username);
        Boolean flag2 = friendsMapper.deletedFriend(username, deletedName);
        return flag1 && flag2;
    }

    @Override
    public Boolean addFriend(FriendDto dto) {
        Boolean flag1 = friendsMapper.addFriend(dto.getFriend_name(),dto.getUser_name());
        Boolean flag2 = friendsMapper.addFriend(dto.getUser_name(),dto.getFriend_name());
        if (flag1 || flag2) return true;
        else return false;
    }

    @Override
    public String verifyFriend(String username,String mine) {
        boolean flag = friendsMapper.selectPerson(username);
        if (flag) {
            // 存在用户了就 去查是否是朋友
            Boolean isFriend = friendsMapper.selectIsFriend(mine, username);
            if (isFriend != null) {
                return "该用户已是你的好友";
            } else if (mine.equals(username)) {
                return "你不能添加你自己";
            } else {
                // 说明该用户不是我的朋友 就直接发请求
                friendsMapper.addFriendRequest(mine,username);
                return "添加消息发送完成";
            }
        } else {
            return "不存在该用户";
        }
    }
}
